namespace SimpleWebServices.Web.Routing
{
    using System.Text.RegularExpressions;

    public sealed class Extension : ValueObject<Extension>
    {
        private static readonly Regex _regex = new Regex(@"^[a-zA-Z0-9]");
        private string _value;

        public Extension()
        {
            this.RegisterProperty(x => x.Value);
        }

        public Extension(string value)
        {
            this.Value = value;
        }

        public string Value
        {
            get
            {
                return this._value;
            }

            set
            {
                if (null != value && _regex.IsMatch(value))
                {
                    this._value = value;
                }
            }
        }

        public static implicit operator Extension(string value)
        {
            return (null == value) ? null as Extension : new Extension(value);
        }

        public static implicit operator string(Extension extension)
        {
            return (null == extension) ? null as string : extension.Value;
        }

        public override string ToString()
        {
            return this.Value;
        }
    }
}